﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace danci
{
    public partial class frmWords : Form
    {
        
        public FRMModel parentFm;
        
        private bool IsNew=false;

        public frmWords()
        {
            InitializeComponent();
        }

        private void frmWords_Load(object sender, EventArgs e)
        {
            this.textBox1.Lines=parentFm.wordlist.ToArray();
            this.labFn.Text = parentFm.CurrentFnUI;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.IsNew = true;
            textBox1.Text = "";
            this.labFn.Text = "<new>";
        }

        private void btnSaveas_Click(object sender, EventArgs e)
        {
            this.saveDial.InitialDirectory = AppConfig.DataRoot;
            if (this.saveDial.ShowDialog() == DialogResult.OK)
            {
                File.WriteAllText(this.saveDial.FileName, textBox1.Text);
                this.IsNew = false;
                Properties.Settings.Default.CurrentList = this.saveDial.FileName;
                Properties.Settings.Default.Save();
                parentFm.setWordList(textBox1.Lines);
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (this.IsNew)
            {
                btnSaveas_Click(sender, e);
            }
            else
            {
                saveCurrent();
                MessageBox.Show("Saved.");
                
            }
        }
        private void saveCurrent()
        {
            parentFm.saveCurrent(textBox1.Lines);
            this.IsNew = false;
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            this.openDial.InitialDirectory = AppConfig.DataRoot;
            this.openDial.FileName = "words.txt";
            this.openDial.DefaultExt = ".txt";
            if (this.openDial.ShowDialog() == DialogResult.OK)
            {
                string filename = this.openDial.FileName;
                Properties.Settings.Default.CurrentList = filename;
                Properties.Settings.Default.Save();
                parentFm.CurrentFnUI = filename;
                textBox1.Text = File.ReadAllText(filename);
                saveCurrent();
                this.parentFm.loadList();
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            labTotal.Text = textBox1.Lines.Length.ToString();
        }
    }
}
